<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
WebScoket测试
<button onclick="closeMsg()">关闭连接</button>
<br/>

发送消息：<input type="text" id="msg"/>
<button onclick="sendMsg()">发送消息</button>
</body>
    <script>

        var websocket=null;

        if('WebSocket' in window){
            websocket=new WebSocket("ws://localhost:8080/msg/1");
        }else{
            alert("浏览器不支持WebSocket");
        }

        websocket.onopen=function (event) {
            console.log("建立连接")
        }
        websocket.onclose=function (event) {
            console.log("连接关闭")
        }


        websocket.onmessage=function (event) {
            alert("收到消息:"+event.data);
        }
        websocket.onerror=function () {
            alert("通信错误");
        }

        function closeMsg() {
            websocket.close();
        }

        function sendMsg() {
            var msg=document.getElementById("msg").value;
            websocket.send(msg);
        }


    </script>
</html>
